package org.sport.dbImplement;

import org.sport.dbInterface.GroLinkUserOperation;
import org.sport.model.entityModel.GrouplinkuserEntity;
import org.sport.model.entityModel.GrouptableEntity;
import org.sport.model.entityModel.UsertableEntity;
import org.sport.repository.GroupLinkUserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Allen on 2017/9/28.
 */
@Service
public class GroLinUOpeImp implements GroLinkUserOperation{

    @Autowired
    GroupLinkUserRepository repository;

    @Autowired
    GroupOpeImp             groupOpeImp;

    @Autowired
    UserOpeImp              userOpeImp;

    @Override
    public boolean addOneData(GrouplinkuserEntity gl) {

        repository.saveAndFlush(gl);
        return true;
    }


    @Override
    public boolean delOneLine(GrouplinkuserEntity gl) {

        repository.delete(gl);
        return true;
    }


    @Override
    public List<UsertableEntity> findOneGroupAllUsers(GrouptableEntity group) {

        List<UsertableEntity> userList = new ArrayList<UsertableEntity>();
        List<GrouplinkuserEntity> glinkList = repository.findGPByGId(group);
        for (int i = 0;i < glinkList.size();i++)
        {
            userList.add(userOpeImp.findUserById(glinkList.get(i).getUsertableByUserId().getId()));
        }

        return userList;
    }


    @Override
    public List<GrouptableEntity> findOneUserAllGroup(UsertableEntity user) {

        List<GrouptableEntity> glist = new ArrayList<GrouptableEntity>();
        List<GrouplinkuserEntity> glinkList = repository.findGByUserId(user);
        for (int i = 0;i < glinkList.size();i++)
        {
            glist.add(groupOpeImp.findById(glinkList.get(i).getGrouptableByGroupId()));
        }

        return glist;
    }
}
